package cn.edu.scau.cmi.chenmiaopeng.controller.compositePattern;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

import java.util.ArrayList;

import cn.edu.scau.cmi.chenmiaopeng.compositePattern.safePattern.Organization_S;
import cn.edu.scau.cmi.chenmiaopeng.compositePattern.safePattern.Team_S;
import cn.edu.scau.cmi.chenmiaopeng.compositePattern.transparentPattern.*;
public class EditOrganizationController {
	    static Stage stage;
	    static boolean isSafe;
	    @FXML
	    private Button confirm;

	    @FXML
	    private Button back;
	    
	    @FXML
	    private TextField input;

	    @FXML
	    private ComboBox<String> comobox;  //用于选择当前选择节点：作为父节点的所有子节点

	    @FXML
	    private Label tips;
	    
	    @FXML
	    private ChoiceBox<String> remove_update;
	    private Team_T fatherOrg_T; //表示当前操作节点的父节点
	    private Team_S fatherOrg_S; //表示当前操作节点的父节点
	    private static String fatherOrgStr;  //当前选择的节点,根据这个字符串再去找当前选择的org
	    private static int isRemove;  //其值表示当前是否进行了移除
	    private static boolean updateSuccess; //表示当前更新操作是否成功
	    private static String updateOrgName;  //表示更新的联系人名
	    private static String modifyOrgName;  //表示用户选择的要做修改的联系人名
	    public static void setSafeType(boolean safe) {
	    	isSafe = safe;
	    }
	    
	    public void setStage(Stage stage) {
	    	this.stage = stage;
	    }

	    public int getIsRemove() {  //0:删除  1：移除  2：修改
	    	return isRemove;
	    }
	    public static String getModifyOrgName() {
    	  return modifyOrgName;
        }
	    public String getUpdateOrgName() {
	    	return updateOrgName;
	    }
	    public boolean getUpdateSuccess() {
	    	return updateSuccess;
	    }
	    @FXML
	    void clickConfirm(ActionEvent event) {
	    	fatherOrgStr = CompositePatternController.getNowSelectOrg();
	    	if(isSafe) {
//	    		if(CompositePatternController.getOriginalOrganization_S().getName().equals(nowOrgStr)) {  //若是根节点，直接显示不能改变
//		    		tips.setText("不能修改根节点！");
//		    	}
//		    	else {
		    		InquiryOrganization inquiryOrg = new InquiryOrganization();
			    	Organization_S nowOrg = inquiryOrg.getOrganization_S(CompositePatternController.getOriginalOrganization_S(), fatherOrgStr);
//			    	if(nowOrg instanceof Personal_S) {
////			    		tips.setText("安全模式下不允许操作个人！");   //禁用确定按钮
//			    	}
			    	Team_S nowOrg_T = (Team_S)nowOrg;
		        	   updateSuccess = false;
		        	   if(comobox.getItems().size()==0) {
		        		   tips.setText("The team you choose has no members!");
		        	   }
		        	   else if(comobox.getSelectionModel().isEmpty()) {  //表示用户没有选择任何子节点
		        		   tips.setText("Please choose a child to modify!");
		        	   }
		        	   else {
		        		   modifyOrgName = comobox.getSelectionModel().getSelectedItem();
	                       ArrayList<Organization_S> orgList_S = nowOrg_T.getOrganizationArrayList();
			        	   if(remove_update.getSelectionModel().getSelectedItem().equals("remove organization")||remove_update.getSelectionModel().getSelectedItem().equals("delete organization")) {  
			        		   //表示去除节点  .selectedItemProperty().equals("remove organization")
	                           
//	                           orgList_S.remove(modifyOrgName);  //是否可行？
	                           
	                           //以下标的方式删除选中的子节点
//	                           int remove_Index = -1;
//	                           for(int i=0;i<orgList_S.size();i++) {
//	                        	   if(orgList_S.get(i).getName().equals(modifyOrgName)) {
//	                        		   remove_Index = i;
//	                        		   break;
//	                        	   }
//	                           }
//	                           orgList_S.remove(remove_Index);
			        		   
			        		   //调用父节点方法删除子节点：
			        		   if(remove_update.getSelectionModel().getSelectedItem().equals("remove organization")) {
			        			   nowOrg_T.setRemoveOrDelete(true);
			        			   isRemove = 1;
			        		   }
			        		   else {
			        			   nowOrg_T.setRemoveOrDelete(false);
			        			   isRemove = 0;
			        		   }
			        		   nowOrg_T.remove(modifyOrgName);
	                           System.out.println("删除后的长度为"+orgList_S.size());
			        		   
			        		   updateSuccess = true;
			        	   }
			        	   else if(remove_update.getSelectionModel().getSelectedItem().equals("update organization")) {  //表示更新节点
			        		   Organization_S modifyOrg = inquiryOrg.getOrganization_S(nowOrg, modifyOrgName);
			        		   isRemove = 2;
			        		   updateOrgName = input.getText();
			        		   if(updateOrgName.isEmpty()) {
			        			   tips.setText("请输入要更改的组织名！");
			        			   updateSuccess = false;
			        		   }
			        		   else{
			        			   modifyOrg.setName(updateOrgName);
			        			   tips.setText("更改组织名成功！");
			        			   updateSuccess = true;
			        		   }
			        	   }
//			        	   else if(remove_update.getSelectionModel().getSelectedItem().equals("delete organization")) {  //表示删除子节点
//			        		   
//			        	   }
		        	   }
	        }
	    	else {
	    		
		    		InquiryOrganization inquiryOrg = new InquiryOrganization();
			    	Organization_T nowOrg = inquiryOrg.getOrganization_T(CompositePatternController.getOriginalOrganization_T(), fatherOrgStr);
			    	
			    	updateSuccess = false;
			    	if(nowOrg instanceof Team_T) {
			    		Team_T nowOrg_T = (Team_T)nowOrg;
			    		if(comobox.getItems().size()==0) {
			        		   tips.setText("The team you choose has no members!");
			        	   }
			        	   else if(comobox.getSelectionModel().isEmpty()) {  //表示用户没有选择任何子节点
			        		   tips.setText("Please choose a child to modify!");
			        	   }
			        	   else {
			        		   modifyOrgName = comobox.getSelectionModel().getSelectedItem();
//		                       ArrayList<Organization_T> orgList_T = nowOrg_T.getOrganizationArrayList();
			        		   if(remove_update.getSelectionModel().getSelectedItem().equals("remove organization")||remove_update.getSelectionModel().getSelectedItem().equals("delete organization")) {  
			        			   //表示去除节点  .selectedItemProperty().equals("remove organization")
//			        			   int remove_Index = -1;
//		                           for(int i=0;i<orgList_T.size();i++) {
//		                        	   if(orgList_T.get(i).getName().equals(modifyChildOrg)) {
//		                        		   remove_Index = i;
//		                        		   break;
//		                        	   }
//		                           }
//		                           orgList_T.remove(remove_Index);
			        			   
			        			   if(remove_update.getSelectionModel().getSelectedItem().equals("remove organization")) {
			        				   nowOrg_T.setRemoveOrDelete(true);
				        			   isRemove = 1;
				        		   }
				        		   else {
				        			   nowOrg_T.setRemoveOrDelete(false);
				        			   isRemove = 0;
				        		   }
			        			   String removeResult = nowOrg.remove(modifyOrgName);
			        			   tips.setText(removeResult);
				        		   updateSuccess = true;
				        	   }
				        	   else if(remove_update.getSelectionModel().getSelectedItem().equals("update organization")) {  //表示更新节点
				        		   isRemove = 2;
				        		   updateOrgName = input.getText();
				        		   if(updateOrgName.isEmpty()) {
				        			   tips.setText("请输入要更改的组织名！");
				        			   updateSuccess = false;
				        		   }
				        		   else{
				        			   nowOrg.setName(updateOrgName);
				        			   tips.setText("更改组织名成功！");
				        			   updateSuccess = true;
				        		   }
			        	   }
//				        	   else if(remove_update.getSelectionModel().getSelectedItem().equals("delete organization")) {  //表示删除子节点
//				        		   
//				        	   }
			        	   }
			    	}
			    	else {  //表示当前选择为个人
			    		if(remove_update.getSelectionModel().getSelectedItem().equals("remove organization"))
				               tips.setText(nowOrg.remove("1"));
				    		else if(remove_update.getSelectionModel().getSelectedItem().equals("update organization"))
				    		tips.setText("The update operation for individual is not supported!");
				    		else if(remove_update.getSelectionModel().getSelectedItem().equals("delete organization")) {  //表示删除子节点
				    			tips.setText("The delete operation for individual is not supported!");
				        	   }
				    	}
			        	
		    	}
//	    	}
	    	
	     }

	    @FXML
	    void clickBack(ActionEvent event) {
        stage.close();
	    }

}
